﻿using CGT.OP.App_Start;
using CGT.OP.Common;
using CGT.OP.Models;
using Newtonsoft.Json;
using OF.Component.BLL;
using OF.Component.Enum;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CGT.OP.Controllers
{
    public class WxController : BaseController
    {
        WxHelper wxObj = new WxHelper();
        #region 菜单
        [Authentication(MenuCode = "cgt40102")]
        public ActionResult OfficialAccountMenu()
        {
            return View();
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetMenuList()
        {
            ResultData<List<MenuInfo>> rData = new ResultData<List<MenuInfo>>();
            List<MenuInfo> mlist = new List<MenuInfo>();
            MenuInfo menuObj = null;
            var list = WxOfficialAccountMenuOperate.QueryList(a => a.IsActive == 1).OrderBy(a => a.MenuId);
            //父级菜单
            var ParentList = from AuthorityMenu in list
                             where AuthorityMenu.MenuParentId == 0
                             select new { AuthorityMenu.MenuName, AuthorityMenu.MenuId, AuthorityMenu.MenuUrl };
            foreach (var pv in ParentList)
            {
                List<ChildMenu> clist = new List<ChildMenu>();
                menuObj = new MenuInfo
                {
                    MenuName = pv.MenuName,
                    MenuID = pv.MenuId,
                    MenuUrl = pv.MenuUrl
                };
                //子级菜单
                var ChildList = from AuthorityMenu in list
                                where AuthorityMenu.MenuParentId == pv.MenuId
                                select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuId, MenuUrl = AuthorityMenu.MenuUrl };
                foreach (var item in ChildList)
                {
                    ChildMenu obj = new ChildMenu
                    {
                        MenuName = item.MenuName,
                        MenuID = item.MenuID,
                        MenuUrl = item.MenuUrl
                    };
                    clist.Add(obj);
                }
                menuObj.ChildMenu = clist;
                mlist.Add(menuObj);
            }
            rData.data = mlist;
            return ReturnResult(rData);
        }

        /// <summary>
        /// 获取父级菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetParentMenuList()
        {
            ResultData<IList<WxOfficialAccountMenuInfo>> rData = new ResultData<IList<WxOfficialAccountMenuInfo>>();
            var list = WxOfficialAccountMenuOperate.QueryList(a => a.IsActive == 1 && a.MenuParentId == 0).OrderBy(a => a.MenuId);
            rData.data = list.ToList();
            return ReturnResult(rData);
        }

        /// <summary>
        /// 根据父级ID获取菜单
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetMenuListByParentId(int parentId)
        {
            ResultData<IList<WxOfficialAccountMenuInfo>> rData = new ResultData<IList<WxOfficialAccountMenuInfo>>();
            IList<WxOfficialAccountMenuInfo> list = WxOfficialAccountMenuOperate.QueryList(a => a.IsActive == 1 && a.MenuParentId == parentId);
            rData.data = list;
            return ReturnResult(rData);
        }

        /// <summary>
        /// 获取菜单类型列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetMenuTypeList()
        {
            ResultData<Dictionary<int, string>> rData = new ResultData<Dictionary<int, string>>();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (var item in Enum.GetValues(typeof(WxMenuTypeEnum)))
            {
                if (!dic.ContainsKey((int)item))
                {
                    dic.Add((int)item, EnumHelper.GetEnumDesc(typeof(WxMenuTypeEnum), (int)item));
                }
            }
            rData.data = dic;
            return ReturnResult(rData);
        }


        [Authentication(MenuCode = "cgt40102")]
        public ActionResult MenuEdit()
        {
            int id = EConvert.ToInt(GetParam("id"));
            int pId = EConvert.ToInt(GetParam("pid"));
            WxOfficialAccountMenuInfo menuObj = null;
            if (id > 0)
            {
                menuObj = WxOfficialAccountMenuOperate.GetByID(id);
                
            }
            if (menuObj == null)
            {
                menuObj = new WxOfficialAccountMenuInfo();
                
            }
            ViewBag.Pid = pId;
            return View(menuObj);
        }

        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authentication(MenuCode = "cgt40102")]
        public ContentResult SaveMenu(MenuModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            WxOfficialAccountMenuInfo info = WxOfficialAccountMenuOperate.GetByID(model.MenuID);
            try
            {
                if (info == null)
                {
                    info = new WxOfficialAccountMenuInfo
                    {
                        MenuName = model.txtName,
                        IsActive = 1,
                        CreatedDate = DateTime.Now,
                        MenuAppId = model.txtAppId,
                        MenuKey = model.txtMenuKey,
                        MenuPagePath = model.txtMenuPagePath,
                        MenuParentId = model.selParentId,
                        MenuType = model.selType,
                        MenuUrl = model.txtURL
                    };
                    object id = WxOfficialAccountMenuOperate.Insert(info);
              

                }
                else
                {

                    info.MenuName = model.txtName;
                    info.MenuAppId = model.txtAppId;
                    info.MenuKey = model.txtMenuKey;
                    info.MenuUrl = model.txtURL;
                    info.MenuType = model.selType;
                    info.MenuPagePath = model.txtMenuPagePath;
                    info.MenuParentId = model.selParentId;
                    WxOfficialAccountMenuOperate.Update(info);
               

                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpPost]
        [Authentication(MenuCode = "cgt40102")]
        public ContentResult DelMenu(int mid)
        {
            ResultData<string> rData = new ResultData<string>();
            if (mid < 1)
            {
                rData.status = -1;
                rData.message = "当前数据不存在";
            }
            else
            {
                WxOfficialAccountMenuInfo menuObj = WxOfficialAccountMenuOperate.GetByID(mid);
                if (menuObj != null)
                {
                    menuObj.IsActive = 0;
                    WxOfficialAccountMenuOperate.Update(menuObj);
                    //删除子菜单
                    if (menuObj.MenuParentId == 0)
                    {
                        WxOfficialAccountMenuOperate.DelMenuByParentId(mid);
                    }

                 
                }
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 生成微信菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authentication(MenuCode = "cgt40102")]
        public ContentResult SendToWxCreateMenu()
        {
            ResultData<string> rData = new ResultData<string>();
            List<WxMenuButton> btnList = new List<WxMenuButton>();
            WxMenuModel btnObj = new WxMenuModel();
            WxMenuButton menuObj = null;
            var list = WxOfficialAccountMenuOperate.QueryList(a => a.IsActive == 1);
            //父级菜单
            var ParentList = list.Where(a => a.MenuParentId == 0 && a.IsActive == 1).ToList();
            foreach (var pv in ParentList)
            {
                List<WxSubMenuButton> subBtnList = new List<WxSubMenuButton>();
                menuObj = new WxMenuButton();
                menuObj.name = pv.MenuName;
                menuObj.type = Enum.GetName(typeof(WxMenuTypeEnum), pv.MenuType);
                if (pv.MenuType == 2)
                {
                    menuObj.url = pv.MenuUrl;
                }
                else if (pv.MenuType == 11)
                {
                    menuObj.appid = pv.MenuAppId;
                    menuObj.url = pv.MenuUrl;
                    menuObj.pagepath = pv.MenuPagePath;
                }
                else
                {
                    menuObj.key = pv.MenuKey;
                }
                //子级菜单
                var ChildList = list.Where(a => a.MenuParentId == pv.MenuId && a.IsActive == 1).ToList();
                foreach (var item in ChildList)
                {
                    WxSubMenuButton obj = new WxSubMenuButton();
                    obj.name = item.MenuName;
                    obj.type = Enum.GetName(typeof(WxMenuTypeEnum), item.MenuType);
                    if (item.MenuType == 2)
                    {
                        obj.url = item.MenuUrl;
                    }
                    else if (item.MenuType == 11)
                    {
                        obj.appid = item.MenuAppId;
                        obj.url = item.MenuUrl;
                        obj.pagepath = item.MenuPagePath;
                    }
                    else
                    {
                        obj.key = item.MenuKey;
                    }
                    subBtnList.Add(obj);
                }
                menuObj.sub_button = subBtnList;
                btnList.Add(menuObj);
            }
            btnObj.button = btnList;
            var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            var json = JsonConvert.SerializeObject(btnObj, Formatting.Indented, jsonSetting);
        
            string result = wxObj.CreateMenu(json);
            WxResult res = JsonConvert.DeserializeObject<WxResult>(result);
            if (res != null)
            {
                if (res.errcode != 0)
                {
                    rData.status = -1;
                    rData.message = res.errmsg;
                }
            }
            else
            {
                rData.status = -1;
                rData.message = "推送微信消息失败";
            }
            return ReturnResult(rData);
        }

        #endregion
    }
}